<template>
  <div>
    <el-tree
      ref="tree"
      :data="list"
      :props="{label:'name',children:'children'}"
      show-checkbox
      :check-strictly="true"
      :default-expand-all="true"
      node-key="id"
    />
    <el-row type="flex" hight="60" justify="center">
      <el-button type="primary" @click="hAssign">确认</el-button>
      <el-button>取消</el-button>
    </el-row>
  </div>
</template>

<script>
import { getPermissionList } from '@/api/permission'
import { transTreeData } from '@/utils/transTreeData'
import { getRoleByIdDetail, assignPerm } from '@/api/role'
export default {
  props: {
    roleId: {
      type: String,
      required: true
    }
  },
  data() {
    return {
      list: [],
      info: []
    }
  },
  created() {
    this.loadPermissionList()
  },
  methods: {
    async loadPermissionList() {
      try {
        const res = await getPermissionList()
        console.log('getPermissionList', res)
        this.list = transTreeData(res.data)
        this.loadRoleByIdDetail()
      } catch (error) {
        console.log(error)
      }
    },
    async loadRoleByIdDetail() {
      try {
        const res = await getRoleByIdDetail(this.roleId)
        console.log('getRoleByIdDetail', res)
        this.info = res.data
        this.$refs.tree.setCheckedKeys(this.info.permIds)
      } catch (error) {
        console.log(error)
      }
    },
    async loadAssignPerm() {
      try {
        const permIds = this.$refs.tree.getCheckedKeys()
        console.log('permIds', permIds)
        const res = await assignPerm({ id: this.roleId, permIds })
        console.log('assignPerm', res)
        this.$emit('close')
      } catch (error) {
        console.log(error)
      }
    },
    hAssign() {
      this.loadAssignPerm()
    }
  }
}

</script>
<style scoped lang='less'>
</style>
